package cn.jx.asc.controller;

import cn.jx.asc.common.constant.Contants;
import cn.jx.asc.common.enums.ResultStatus;
import cn.jx.asc.common.response.Result;
import cn.jx.asc.common.response.ResultUtils;
import cn.jx.asc.common.utils.MD5Utils;
import cn.jx.asc.pojo.bo.RegistUserBO;
import cn.jx.asc.pojo.bo.UserInfoBO;
import cn.jx.asc.pojo.entity.User;
import cn.jx.asc.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 用户管理
 *
 * @author lhg
 */

@Api(value = "用户管理", tags = {"用户管理的相关接口"})
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequiresRoles("ADMIN")
    @ApiOperation(value = "查询所有用户", notes = "查询所有用户", httpMethod = "GET")
    @GetMapping("/queryAllUsers")
    public Result queryAllUsers(
            @ApiParam(name = "current",value = "第几页")
                    Long current,
            @ApiParam(name = "size",value = "条数")
                    Long size){
        if (current == null){
            current = Contants.CURRENT;
        }

        if (size == null){
            size = Contants.SIZE;
        }
        final UserInfoBO result = userService.queryAllUsers(current,size);

        return ResultUtils.resultWithData(ResultStatus.SUCCESS,result);
    }

    @RequiresRoles("ADMIN")
    @ApiOperation(value = "修改用户密码", notes = "修改用户密码", httpMethod = "POST")
    @PostMapping("/modifyPasswordById")
    public Result modifyPasswordById(
            @ApiParam(name = "userId",value = "用户id",required = true)
            @RequestParam String userId,
            @ApiParam(name = "newPassword",value = "用户新密码",required = true)
            @RequestParam String newPassword,
            @ApiParam(name = "confirmPwd",value = "确认密码",required = true)
            @RequestParam String confirmPwd) {
        if (StringUtils.isBlank(userId)) {
            return ResultUtils.resultWithoutData(ResultStatus.NOT_ID);
        }

        if (StringUtils.isBlank(newPassword) || StringUtils.isBlank(confirmPwd)) {
            return ResultUtils.resultWithoutData(ResultStatus.NOT_USERNAME_PASSWORD);
        }
        if (!newPassword.equals(confirmPwd)) {
            return ResultUtils.resultWithoutData(ResultStatus.INCONSISTENT_PASSWORDS);
        }
        Boolean result = false;
        try {
            result = userService.modifyPasswordById(userId, MD5Utils.getMD5Str(newPassword));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtils.resultWithData(ResultStatus.SUCCESS,result);
    }

    @RequiresRoles("ADMIN")
    @ApiOperation(value = "重置密码", notes = "重置密码", httpMethod = "POST")

    @PostMapping("/resetPasswordById")
    public Result resetPasswordById(
            @ApiParam(name = "userId",value = "用户id",required = true)
            @RequestParam String userId) {
        if (StringUtils.isBlank(userId)) {
            return ResultUtils.resultWithoutData(ResultStatus.NOT_ID);
        }
        String newPassword = Contants.DEFAULTPASSWORD;
        Boolean result = false;
        try {
            result = userService.modifyPasswordById(userId, MD5Utils.getMD5Str(newPassword));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtils.resultWithData(ResultStatus.SUCCESS,result);
    }


    @RequiresRoles("ADMIN")
    @ApiOperation(value = "修改用户账号状态", notes = "修改用户账号状态", httpMethod = "POST")
    @PostMapping("/modifyUserStatusById")
    public Result modifyUserStatusById(
            @ApiParam(name = "userId",value = "用户id",required = true)
            @RequestParam String userId,
            @ApiParam(name = "userStatus",value = "用户状态",required = true)
            @RequestParam Integer userStatus) {
        if (StringUtils.isBlank(userId)) {
            return ResultUtils.resultWithoutData(ResultStatus.NOT_ID);
        }

        if ( ! userStatus.equals( Contants.REGISTER ) && ! userStatus.equals( Contants.NORMAL ) && ! userStatus.equals( Contants.DEACTIVATE ) ){
            return ResultUtils.resultWithoutData(ResultStatus.USER_IS_NOT_ACTIVATED);
        }
        Boolean result = userService.modifyUserStatusById(userId, userStatus);
        return ResultUtils.resultWithData(ResultStatus.SUCCESS,result);
    }

    @RequiresRoles("ADMIN")
    @ApiOperation(value = "删除用户", notes = "删除用户", httpMethod = "DELETE")
    @DeleteMapping("/deleteUserById")
    public Result deleteUserById(
            @ApiParam(name = "userId",value = "用户id",required = true)
            @RequestParam String userId) {

        if (StringUtils.isBlank(userId)) {
            return ResultUtils.resultWithoutData(ResultStatus.NOT_ID);
        }
        Boolean result = userService.deleteUserById(userId);
        return ResultUtils.resultWithData(ResultStatus.SUCCESS,result);
    }

    @RequiresRoles("ADMIN")
    @ApiOperation(value = "通过id查询用户信息", notes = "通过id查询用户信息", httpMethod = "GET")
    @GetMapping("/queryUserById")
    public Result queryUserById(
            @ApiParam(name = "userId",value = "用户id",required = true)
            @RequestParam String userId) {

        if (StringUtils.isBlank(userId)) {
            return ResultUtils.resultWithoutData(ResultStatus.NOT_ID);
        }
        User user = userService.queryUserById(userId);
        user.setPassword(null);
        return ResultUtils.resultWithData(ResultStatus.SUCCESS,user);
    }


    @RequiresRoles("ADMIN")
    @ApiOperation(value = "查询所有注册状态的用户", notes = "查询所有注册状态的用户", httpMethod = "GET")
    @GetMapping("/queryRegisterUser")
    public Result queryRegisterUser(
            @ApiParam(name = "current",value = "第几页")
             Long current,
            @ApiParam(name = "size",value = "条数")
             Long size) {
        if (current == null){
            current = Contants.CURRENT;
        }

        if (size == null){
            size = Contants.SIZE;
        }
        final UserInfoBO userInfoBO = userService.queryRegisterUser(current, size);
        return ResultUtils.resultWithData(ResultStatus.SUCCESS,userInfoBO);
    }

    @RequiresRoles("ADMIN")
    @ApiOperation(value = "新增用户", notes = "新增用户", httpMethod = "POST")
    @PostMapping("/insertUser")
    public Result insertUser(
            @ApiParam(name = "newUser",value = "新增用户信息对象",required = true)
            @RequestBody RegistUserBO newUser) {
        String newUsername = newUser.getUsername();
        String password = newUser.getPassword();
        String confirmPwd = newUser.getConfirmPassword();

        if (StringUtils.isBlank(newUser.getUserId())){
            return ResultUtils.resultWithoutData(ResultStatus.NOT_EMPLOYEE_NUMBER);
        }
        if (StringUtils.isBlank(newUser.getUsername()) || StringUtils.isBlank(password) || StringUtils.isBlank(confirmPwd) ){
            return ResultUtils.resultWithoutData(ResultStatus.NOT_USERNAME_PASSWORD);
        }
        if (StringUtils.isBlank(newUser.getRealname())){
            return ResultUtils.resultWithoutData(ResultStatus.REAL_NAME_IS_EMPTY);
        }
        final boolean usernameIsExist = userService.queryUsernameIsExist(newUsername);
        if (usernameIsExist){
            return ResultUtils.resultWithoutData(ResultStatus.USERNAME_EXISTS);
        }
        if (password.length()< Contants.MIN_PASSWORD_LENGTH){
            return ResultUtils.resultWithoutData(ResultStatus.INSECURE_PASSWORD);
        }
        if (!password.equals(confirmPwd)){
            return ResultUtils.resultWithoutData(ResultStatus.INCONSISTENT_PASSWORDS);
        }
        userService.addUser(newUser);
        return ResultUtils.resultWithoutData(ResultStatus.CREATED);
    }
}
